package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCUtils {
    /**
     * 获取连接
     *
     */
    public static Connection getConnection()
    {
        String driverName = "com.mysql.jdbc.Driver";
        String db_host;
        String db_port;
        String db_user;
        String db_pass;
        String db_name;
        String url;

        db_host = "121.42.148.179";
        db_port = "3306";
        db_name = "student";
        db_user = "root";
        db_pass = "1234";

        url = "jdbc:mysql://"
                + db_host + ":"
                + db_port + "/"
                + db_name + "?"
                + "user=" + db_user + "&"
                + "password=" + db_pass
                + "&useUnicode=true&characterEncoding=UTF8";

        Connection con = null ;
        try {

            Class.forName(driverName);
            con = DriverManager.getConnection(url);
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return con ;

    }

    /**
     * 关闭连接
     */
    public static void free(ResultSet rs, Statement sta , Connection con)
    {
        try {
            if(null != rs)
            {
                rs.close();
                rs = null ;
            }

            if(null != sta)
            {
                sta.close();
                sta = null ;
            }

            if(null != con)
            {
                con.close();
                con = null ;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
